//
//  QLDatePickerView.m
//  青旅O2O
//
//  Created by qlwl on 16/5/12.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLDatePickerView.h"
#import "NSDate+time.h"


#define PickerShowHeight (230)

@interface QLDatePickerView ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;
@property (weak, nonatomic) IBOutlet UILabel *titLab;
@property (nonatomic, strong) UIButton * bgBtn ;//用于显示底部覆盖


@end

@implementation QLDatePickerView

+(instancetype)DatePickerView
{
    QLDatePickerView *pickerView = [[[NSBundle mainBundle] loadNibNamed:@"QLDatePickerView" owner:nil options:nil] lastObject];
    
    pickerView.frame =CGRectMake(0, 0, iphoneWidth, PickerShowHeight);
    pickerView.date = [NSDate date];
    return pickerView;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        _isShowing = NO;
        self.bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bgBtn addTarget:self action:@selector(bgBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (IBAction)finishBtnClick:(id)sender {
    if(self.finishBtnClick){
        self.finishBtnClick(_date);
    }
    
    if ([self.responderView respondsToSelector:@selector(resignFirstResponder)]) {
        [self.responderView resignFirstResponder];
    }else{
        [self remove];
    }
}
- (IBAction)datePickerChangedClick:(id)sender {
    UIDatePicker *datePicker = (UIDatePicker *)sender;
    NSDate *date = datePicker.date;
    _date = date;
    if (self.datePickerChanged) {
        self.datePickerChanged(date);
    }
}
// 类似取消
- (void)bgBtnClick
{
    [self remove];
}
- (void)setDate:(NSDate *)date
{
    _date = date;
    _datePicker.date = _date;
}
- (void)setDate:(NSDate *)date animated:(BOOL)animated
{
    _date = date;
    [_datePicker setDate:date animated:animated];
}
- (void)setMaximumDate:(NSDate *)maximumDate
{
    _maximumDate = maximumDate;
    _datePicker.maximumDate = _maximumDate;
}
- (void)setMinimumDate:(NSDate *)minimumDate
{
    _minimumDate = minimumDate;
    _datePicker.minimumDate = _minimumDate;
}
- (void)setDatePickerMode:(UIDatePickerMode)datePickerMode
{
    _datePickerMode = datePickerMode;
    _datePicker.datePickerMode = _datePickerMode;
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titLab.text = title;
}


#pragma mark 直接显示方法 用这个方法显示的才会显示bgBtn
- (void)showInWindow
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.bgBtn.frame = window.bounds;
    [window addSubview:self.bgBtn];
    _isShowing = YES;
    [window addSubview:self];
    self.frame = CGRectMake(0, iphoneHeight, iphoneWidth, PickerShowHeight);
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, iphoneHeight - PickerShowHeight, iphoneWidth, PickerShowHeight);
    }];
}
- (void)remove
{
    [self.bgBtn removeFromSuperview];
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, iphoneHeight, iphoneWidth, PickerShowHeight);
    } completion:^(BOOL finished) {
        _isShowing = NO;
        [self removeFromSuperview];
    }];
}


@end
